"""
封装的方法
详细解释在header.py
作用是启动指定浏览器并且不会关闭页面
"""

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service


# 从你的constants.py导入路径 测试用
from config.constants import chrome_path, driver_path


def init_chrome_browser(chrome_path: str, driver_path: str, detach: bool = True) -> webdriver.Chrome:
    """
    初始化可复用的Chrome浏览器实例

    参数:
        chrome_path: Chrome可执行文件路径
        driver_path: chromedriver路径
        detach: 是否保持浏览器打开(默认True)

    返回:
        WebDriver实例
    """
    # 创建配置项
    chrome_options = Options()
    if detach:
        chrome_options.add_experimental_option("detach", True)
    chrome_options.binary_location = chrome_path

    # 初始化服务
    chrome_service = Service(executable_path=driver_path)

    # 创建浏览器实例
    driver = webdriver.Chrome(service=chrome_service, options=chrome_options)
    return driver

# 打开百度测试
# driver = init_chrome_browser(chrome_path, driver_path)
# driver.get("https://www.baidu.com")
